<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
    public function index(){
       if(IS_POST){
           $data = I('post.');
           if($_FILES){
               $file = $_FILES['img'];
               $name = $file['name'];
               $type = strtolower(substr($name,strrpos($name,'.')+1)); 
               $allow_type = array('jpg','jpeg','gif','png'); //定义允许上传的类型
                //判断文件类型是否被允许上传
                if(!in_array($type, $allow_type)){
                  $this->error('请上传图片！');
                  return ;
                }
                $upfile=SITE_FILE.'/Uploads'; //图片目录路径
                  if(!file_exists($upfile)){  // 判断存放文件目录是否存在
                    mkdir($upfile,0777,true);
                    } 
                   $imageSize=getimagesize($file['tmp_name']);
                    $img=$imageSize[0].'*'.$imageSize[1];
                    $fname=$file['name'];
                    $ftype=explode('.',$fname);
                    $picName=$upfile."/".time().$fname;
                    if(file_exists($picName)){
                     echo "<font color='#FF0000'>同文件名已存在！</font>";
                     exit;
                      }
                      
                        if(!move_uploaded_file($file['tmp_name'],$picName)){  
                        echo "<font color='#FF0000'>移动文件出错！</font>";
                        exit;
                        }else{
                          if(oss_upload($picName)){
                           echo  get_url($picName);
                          }
                          
                        }
                        
                   echo "<font color='#FF0000'>上传成功！</font>";
                        exit;
              
           }
          
       }
        $this->display();
    }
}